<?php
   require "./include/common.inc.php";
   require "./include/pagination/pagination.func.php";

   $id = $_COOKIE['uid'];
   $indentity = $_COOKIE['isLogin'];  

   // if( isset($_GET) ){
   // 	   $val = $_GET['time'];
   // 	   // var_dump($val);
   // }

   //收件箱
   $sql = "SELECT count(1) c FROM `message` WHERE `to_id`= '$id'";

   //总数据量
   $count = get_one($sql)['c'];

   //每一页数字页码数量
   $size = 5;
  
   //每一页显示数据量
   $limit = 3;

   //总页码量

   //获取当前页码[限制页码的范围 [1 < 当前页码 < 最大页码] ]
   if( isset($_GET['page']) && $_GET['page'] > 0 ){
     $cur = $_GET['page'];
   }else{
     $cur = 1;
   }

   //根据页码显示显示对应的列表数据
   $offset = ( $cur-1 ) * $limit;

   //  先查学生发给我的
   /*
   SELECT s.`name`,m.`title`,m.`content`,m.`status` FROM `message` m LEFT JOIN `student` s ON m.`from_id`=s.`id` WHERE `to`='$indentity' AND m.`to_id`=$tid AND `from`='student'
    */ 
   // 再查老师发给我的
   /*
   SELECT t.`name`,m.`title`,m.`content`,m.`status` FROM `message` m LEFT JOIN `teacher` t ON m.`from_id`=t.`id` WHERE `to`='$indentity' AND m.`to_id`=$tid AND `from`='teacher'
   */

   //收件箱
   $sql = "SELECT s.`id`,s.`name`,m.`title`,m.`addtime`,m.`content`,m.`status` FROM `message` m LEFT JOIN `student` s ON m.`from_id`=s.`id` WHERE `to`='$indentity' AND m.`to_id`= '$id' AND `from`='student' UNION ALL SELECT t.`id`,t.`name`,m.`title`,m.`addtime`,m.`content`,m.`status` FROM `message` m LEFT JOIN `teacher` t ON m.`from_id`=t.`id` WHERE `to`='$indentity' AND m.`to_id`= '$id' AND `from`='teacher' ORDER BY `id` LIMIT $offset,$limit";
   $info = get_all($sql);


?>
<!DOCTYPE html>
<html>
<head>
	<title>我的留言</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- loading mui -->
	<link rel="stylesheet" type="text/css" href="css/mui.min.css">
	<!-- loading mui.picker.min.css -->
	<link rel="stylesheet" type="text/css" href="css/mui.picker.min.css">
	<!-- loading mui.poppicker.css -->
	<link rel="stylesheet" type="text/css" href="css/mui.poppicker.css">	
	<!-- custorm style -->
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<link rel="stylesheet" href="./include/pagination/page.css">
</head>
<body>
	<!-- 导航栏 -->
	<header id="header" class="mui-bar mui-bar-nav">
		<h1 class="mui-title">奇猿老师管理系统</h1>
		<a class="mui-action-back mui-btn mui-btn-blue mui-btn-link mui-btn-nav mui-pull-left" href="javascript:history.go(-1)"><span class="mui-icon mui-icon-left-nav"></span></a>
		<a class="mui-icon mui-icon-bars mui-pull-right" href="#topPopover"></a>
	</header>
	<!-- 右上角弹出菜单 -->
	<div id="topPopover" class="mui-popover">
			<div class="mui-popover-arrow"></div>
			<div class="mui-scroll-wrapper">
				<div class="mui-scroll">
					<ul class="mui-table-view">
						<li class="mui-table-view-cell"><a href="teacher_index.php">首页</a>
						</li>
						<li class="mui-table-view-cell"><a href="query.html">我要查询</a>
						</li>
						<li class="mui-table-view-cell"><a href="rate.html">我要评价</a>
						</li>
						<li class="mui-table-view-cell"><a href="enroll.html">我要选课</a>
						</li>
						<li class="mui-table-view-cell"><a href="teacher_message.php">我要留言</a>
						</li>						
						<li class="mui-table-view-cell"><a href="teacher_center.php">个人中心</a>
						</li>
					</ul>
				</div>
			</div>
		</div>
	</div>
	<!-- 主内容部分 -->
	<div class="content">
		<section class="xueqi">
			<div class="class">
				<label id="classResult">留言板</label>
			</div>
		</section>
		<section class="rate_group">
			<div class="mui-input-row">
				<input type="button" class="msg-list-btn mui-btn mui-btn-block" data-input-clear="5" id="showUserPicker" value='收件箱' >
			</div>
				<table>
					<tr>
						<th>发件人名称</th>
						<th>标题</th>
						<th>接收时间</th>
						<th>
						    <span style="color:#007aff;">已读</span> / 
						    <span style="color: red;">未读</span> 
						</th>
					</tr>
					<?php foreach($info as $item){ ?>
					<tr>
						<td><?php echo $item['name']; ?></td>
						<td><?php echo $item['title']; ?></td>
						<td><?php echo date('Y-m-d H:i',$item['addtime']); ?></td>
						<td align="center">
						<a href="get_msg.php?addtime=<?=$item["addtime"]; ?>" <?=$item['status']!=1?'style="color:red;"':''; ?>>详情</a>
						</td>
					</tr>
					<?php } ?>
				</table>
				
			<ul class="mui-pagination">
				<?php echo pagination($count,$cur,$limit,$size,null); ?>
			</ul>
	</div>
	<script src="js/mui.min.js"></script>
	<!-- loading mui.picker.min.js -->
	<script type="text/javascript" src="js/mui.picker.min.js"></script>
	<!-- loading mui.poppicker.js -->
	<script type="text/javascript" src="js/mui.poppicker.js"></script>
	<script type="text/javascript">
		(function($, doc) {
			$.init();
			$.ready(function() {
				//普通示例
				var userPicker = new $.PopPicker();
				userPicker.setData([{
					value: 'get',
					text: '收件箱'
				}, {
					value: 'send',
					text: '发件箱'
				}]);
				var showUserPickerButton = doc.getElementById('showUserPicker');
				var userInput = doc.querySelector('input[name=identity]');
				var userResult = doc.getElementById('userResult');
				showUserPickerButton.addEventListener('tap', function(event) {
					userPicker.show(function(items) {
						showUserPickerButton.value = items[0].text;
						location.href="teacher_message_list2.php?type="+items[0].value;
						//showUserPicker.value = items[0].value;
						//返回 false 可以阻止选择框的关闭
						// return false;
					});
				}, false);
			});
		})(mui, document);
	</script>	
</body>
</html>